package com.tancire.works.service.impl;

import java.util.List;
import javax.annotation.Resource;

import cn.hutool.core.util.IdUtil;
import com.tancire.common.core.domain.entity.SysUser;
import com.tancire.common.enums.MsgCategoryEnum;
import com.tancire.common.enums.MsgScopeEnum;
import com.tancire.common.enums.MsgTypeEnum;
import com.tancire.common.enums.UserSource;
import com.tancire.common.utils.DateUtils;
import com.tancire.common.utils.SecurityUtils;
import com.tancire.message.domain.MsgMessage;
import com.tancire.message.service.IMsgMessageService;
import com.tancire.works.domain.SideWork;
import com.tancire.works.mapper.SideWorkMapper;
import org.springframework.stereotype.Service;
import com.tancire.works.mapper.SideWorkEnrollMapper;
import com.tancire.works.domain.SideWorkEnroll;
import com.tancire.works.service.ISideWorkEnrollService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 兼职报名Service业务层处理
 *
 * @author tancire
 * @date 2024-03-11
 */
@Service
public class SideWorkEnrollServiceImpl implements ISideWorkEnrollService {

    @Resource
    private SideWorkEnrollMapper sideWorkEnrollMapper;

    @Resource
    private SideWorkMapper sideWorkMapper;

    @Resource
    private IMsgMessageService msgMessageService;

    /**
     * 查询兼职报名
     *
     * @param id 兼职报名主键
     * @return 兼职报名
     */
    @Override
    public SideWorkEnroll selectSideWorkEnrollById(Long id) {
        return sideWorkEnrollMapper.selectSideWorkEnrollById(id);
    }

    /**
     * 查询兼职报名列表
     *
     * @param sideWorkEnroll 兼职报名
     * @return 兼职报名
     */
    @Override
    public List<SideWorkEnroll> selectSideWorkEnrollList(SideWorkEnroll sideWorkEnroll) {
        return sideWorkEnrollMapper.selectSideWorkEnrollList(sideWorkEnroll);
    }

    /**
     * 新增兼职报名
     *
     * @param sideWorkEnroll 兼职报名
     * @return 结果
     */
    @Transactional
    @Override
    public int insertSideWorkEnroll(SideWorkEnroll sideWorkEnroll) {
        sideWorkEnroll.setCreateTime(DateUtils.getNowDate());
        sideWorkEnroll.setId(IdUtil.getSnowflakeNextId());
        // 初始化基础信息
        SideWork sideWork = this.sideWorkMapper.selectSideWorkById(sideWorkEnroll.getWorkId());
        sideWorkEnroll.setWorkName(sideWork.getTitle());
        sideWorkEnroll.setWorkCompanyId(sideWork.getCompanyId());
        sideWorkEnroll.setWorkCompanyName(sideWork.getCompanyName());
        // 初始化会员信息
        SysUser user = SecurityUtils.getLoginUser().getUser();
        if (UserSource.MEMBER.getCode() == user.getUserSource()) {
            sideWorkEnroll.setUserId(user.getUserId());
            sideWorkEnroll.setUserAccount(user.getAccount());
        }
        // 其他信息
        sideWorkEnroll.setCreateBy(user.getUserId() + "");
        int count = sideWorkEnrollMapper.insertSideWorkEnroll(sideWorkEnroll);
        if (count == 1) {
            // 发送报名成功消息
            MsgMessage message = new MsgMessage();
            // 初始化信息
            message.setRevUserId(user.getUserId());
            message.setRevUserName(user.getNickName());
            // 0表示系统
            message.setUserId(0L);
            // 标记为通知
            message.setMsgCategory(MsgCategoryEnum.NOTICE.getCode());
            message.setMsgType(MsgTypeEnum.TEXT.getCode());
            message.setMsgScope(MsgScopeEnum.SINGLE.getCode());
            message.setMsgContent("您报名的岗位‘" + sideWorkEnroll.getWorkName() + "’已经报名成功，请耐心等待联系!");
            this.msgMessageService.sendMessage(message);
        }
        return count;
    }

    /**
     * 修改兼职报名
     *
     * @param sideWorkEnroll 兼职报名
     * @return 结果
     */
    @Override
    public int updateSideWorkEnroll(SideWorkEnroll sideWorkEnroll) {
        sideWorkEnroll.setUpdateTime(DateUtils.getNowDate());
        return sideWorkEnrollMapper.updateSideWorkEnroll(sideWorkEnroll);
    }

    /**
     * 批量删除兼职报名
     *
     * @param ids 需要删除的兼职报名主键
     * @return 结果
     */
    @Override
    public int deleteSideWorkEnrollByIds(Long[] ids) {
        return sideWorkEnrollMapper.deleteSideWorkEnrollByIds(ids);
    }

    /**
     * 删除兼职报名信息
     *
     * @param id 兼职报名主键
     * @return 结果
     */
    @Override
    public int deleteSideWorkEnrollById(Long id) {
        return sideWorkEnrollMapper.deleteSideWorkEnrollById(id);
    }
}
